home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter3 / isohex3_6 / isohex3_6.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-18  |  6.4 KB  |  252 lines

  1. /*****************************************************************************
  2. IsoHex3_6.cpp
  3. Ernest S. Pazera
  4. 18MAY2000
  5. Start a WIN32 Application Workspace, add in this file
  6. No other libs are required
  7. *****************************************************************************/
  8.  
  9. //////////////////////////////////////////////////////////////////////////////
  10. //INCLUDES
  11. //////////////////////////////////////////////////////////////////////////////
  12. #define WIN32_LEAN_AND_MEAN  
  13.  
  14. #include <windows.h>   
  15.  
  16. //////////////////////////////////////////////////////////////////////////////
  17. //DEFINES
  18. //////////////////////////////////////////////////////////////////////////////
  19. //name for our window class
  20. #define WINDOWCLASS "ISOHEX3"
  21. //title of the application
  22. #define WINDOWTITLE "IsoHex 3-6"
  23.  
  24. //bitmap sizes
  25. const int BITMAPHEIGHT=32;
  26. const int BITMAPWIDTH=64;
  27.  
  28. //////////////////////////////////////////////////////////////////////////////
  29. //PROTOTYPES
  30. //////////////////////////////////////////////////////////////////////////////
  31. bool Prog_Init();//game data initalizer
  32. void Prog_Loop();//main game loop
  33. void Prog_Done();//game clean up
  34.  
  35. //////////////////////////////////////////////////////////////////////////////
  36. //GLOBALS
  37. //////////////////////////////////////////////////////////////////////////////
  38. HINSTANCE hInstMain=NULL;//main application handle
  39. HWND hWndMain=NULL;//handle to our main window
  40. //memory dcs
  41. HDC hdcMem=NULL;
  42. HDC hdcMask=NULL;
  43. //bitmaps
  44. HBITMAP hbmNew=NULL;
  45. HBITMAP hbmOld=NULL;
  46. HBITMAP hbmMaskNew=NULL;
  47. HBITMAP hbmMaskOld=NULL;
  48.  
  49. //////////////////////////////////////////////////////////////////////////////
  50. //WINDOWPROC
  51. //////////////////////////////////////////////////////////////////////////////
  52. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  53. {
  54.     //which message did we get?
  55.     switch(uMsg)
  56.     {
  57.     case WM_LBUTTONDOWN:
  58.         {
  59.             //borrow dc from main window
  60.             HDC hdc=GetDC(hWndMain);
  61.  
  62.             //blit from the memory dc to the window's dc
  63.             BitBlt(hdc,LOWORD(lParam)-BITMAPWIDTH/2,HIWORD(lParam)-BITMAPHEIGHT/2,BITMAPWIDTH,BITMAPHEIGHT,hdcMask,0,0,SRCAND);
  64.             BitBlt(hdc,LOWORD(lParam)-BITMAPWIDTH/2,HIWORD(lParam)-BITMAPHEIGHT/2,BITMAPWIDTH,BITMAPHEIGHT,hdcMem,0,0,SRCPAINT);
  65.  
  66.             //return the borrowed dc to the system
  67.             ReleaseDC(hWndMain,hdc);
  68.  
  69.             //handled, so return 0
  70.             return(0);
  71.         }break;
  72.     case WM_DESTROY://the window is being destroyed
  73.         {
  74.  
  75.             //tell the application we are quitting
  76.             PostQuitMessage(0);
  77.  
  78.             //handled message, so return 0
  79.             return(0);
  80.  
  81.         }break;
  82.     case WM_PAINT://the window needs repainting
  83.         {
  84.             //a variable needed for painting information
  85.             PAINTSTRUCT ps;
  86.             
  87.             //start painting
  88.             HDC hdc=BeginPaint(hwnd,&ps);
  89.  
  90.             /////////////////////////////
  91.             //painting code would go here
  92.             /////////////////////////////
  93.  
  94.             //end painting
  95.             EndPaint(hwnd,&ps);
  96.                         
  97.             //handled message, so return 0
  98.             return(0);
  99.         }break;
  100.     }
  101.  
  102.     //pass along any other message to default message handler
  103.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  104. }
  105.  
  106.  
  107. //////////////////////////////////////////////////////////////////////////////
  108. //WINMAIN
  109. //////////////////////////////////////////////////////////////////////////////
  110. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  111. {
  112.     //assign instance to global variable
  113.     hInstMain=hInstance;
  114.  
  115.     //create window class
  116.     WNDCLASSEX wcx;
  117.  
  118.     //set the size of the structure
  119.     wcx.cbSize=sizeof(WNDCLASSEX);
  120.  
  121.     //class style
  122.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  123.  
  124.     //window procedure
  125.     wcx.lpfnWndProc=TheWindowProc;
  126.  
  127.     //class extra
  128.     wcx.cbClsExtra=0;
  129.  
  130.     //window extra
  131.     wcx.cbWndExtra=0;
  132.  
  133.     //application handle
  134.     wcx.hInstance=hInstMain;
  135.  
  136.     //icon
  137.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  138.  
  139.     //cursor
  140.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  141.  
  142.     //background color
  143.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  144.  
  145.     //menu
  146.     wcx.lpszMenuName=NULL;
  147.  
  148.     //class name
  149.     wcx.lpszClassName=WINDOWCLASS;
  150.  
  151.     //small icon
  152.     wcx.hIconSm=NULL;
  153.  
  154.     //register the window class, return 0 if not successful
  155.     if(!RegisterClassEx(&wcx)) return(0);
  156.  
  157.     //create main window
  158.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  159.  
  160.     //error check
  161.     if(!hWndMain) return(0);
  162.  
  163.     //if program initialization failed, then return with 0
  164.     if(!Prog_Init()) return(0);
  165.  
  166.     //message structure
  167.     MSG msg;
  168.  
  169.     //message pump
  170.     for(;;)    
  171.     {
  172.         //look for a message
  173.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  174.         {
  175.             //there is a message
  176.  
  177.             //check that we arent quitting
  178.             if(msg.message==WM_QUIT) break;
  179.             
  180.             //translate message
  181.             TranslateMessage(&msg);
  182.  
  183.             //dispatch message
  184.             DispatchMessage(&msg);
  185.         }
  186.  
  187.         //run main game loop
  188.         Prog_Loop();
  189.     }
  190.     
  191.     //clean up program data
  192.     Prog_Done();
  193.  
  194.     //return the wparam from the WM_QUIT message
  195.     return(msg.wParam);
  196. }
  197.  
  198. //////////////////////////////////////////////////////////////////////////////
  199. //INITIALIZATION
  200. //////////////////////////////////////////////////////////////////////////////
  201. bool Prog_Init()
  202. {
  203.     //borrow dc from main window
  204.     HDC hdc=GetDC(hWndMain);
  205.  
  206.     //create a memory dc
  207.     hdcMem=CreateCompatibleDC(hdc);
  208.     hdcMask=CreateCompatibleDC(hdc);
  209.  
  210.     //return dc to system
  211.     ReleaseDC(hWndMain,hdc);
  212.  
  213.     //load in the bitmap
  214.     hbmNew=(HBITMAP)LoadImage(NULL,"IsoHex3_6-1.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
  215.     hbmMaskNew=(HBITMAP)LoadImage(NULL,"IsoHex3_6-2.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
  216.  
  217.     //select bitmap into memory dc
  218.     hbmOld=(HBITMAP)SelectObject(hdcMem,hbmNew);
  219.     hbmMaskOld=(HBITMAP)SelectObject(hdcMask,hbmMaskNew);
  220.  
  221.     return(true);//return success
  222. }
  223.  
  224. //////////////////////////////////////////////////////////////////////////////
  225. //CLEANUP
  226. //////////////////////////////////////////////////////////////////////////////
  227. void Prog_Done()
  228. {
  229.     //restore old bitmap to memory dc
  230.     SelectObject(hdcMem,hbmOld);
  231.     SelectObject(hdcMask,hbmMaskOld);
  232.  
  233.     //delete bitmap
  234.     DeleteObject(hbmNew);
  235.     DeleteObject(hbmMaskNew);
  236.  
  237.     //delete dc
  238.     DeleteDC(hdcMem);
  239.     DeleteDC(hdcMask);
  240. }
  241.  
  242. //////////////////////////////////////////////////////////////////////////////
  243. //MAIN GAME LOOP
  244. //////////////////////////////////////////////////////////////////////////////
  245. void Prog_Loop()
  246. {
  247.     ///////////////////////////
  248.     //main game logic goes here
  249.     ///////////////////////////
  250. }
  251.  
  252.